<?php

class list_handler extends _site_handler {
	public $QueryExtra;
	public $SingleEditLink = 'single_edit';
	public $DeleteLink = 'delete';
	public $BackLink = '';
	public $AddLink = 'add';
	public $SubSectionLink = '';
	public $QueryOrderBy = '';
	public $QueryAscDesc = '';
	public $RemoveSortLink = '';
	
	public $EditBaseLink = 'edit';
	public $OrderByLinks = array();
	
	public $EditLinks = array();
	public $DeleteLinks = array();

	public $Page = 1;
	public $ItemCount = 30;
	public $TotalCount;
	public $PageLink = 'list';

	public function __construct($m, $view) {
		$this->InitializeSite($m);
		$db_info = $this->Lade();
	
		$section = @$_GET['section'];
		$orderby = @$_GET['order'];
		$ascdesc = @$_GET['ascdesc'];
		$section_info = @$db_info->Sections[$section];

		$page = intval(@$_GET['page']);
		if ( $page ) $this->Page = $page;

		$item_count = intval(@$_GET['item_count']);
		if ( $item_count ) $this->ItemCount = $item_count;

		/* if ( self::$User->Rights[$section] == NULL || !self::$User->Rights[$section]->Read ) {
			return;
			// no access
		} */
	
		$pk = @$_GET['pk'];
	
		$subsection_info = NULL;
	
		if ( $section_info != NULL ) {
			$subsection = @$_GET['subsection'];
			$suborderby = @$_GET['suborder'];
			$subascdesc = @$_GET['subascdesc'];
	
			if ( $section_info->SubSection != NULL && $section_info->SubSection->Name == $subsection ) $subsection_info = $section_info->SubSection;
	
			if ( $subsection_info != NULL ) {
				$this->EditBaseLink .= '?section=' . $section_info->Name . '&pk=' . $pk;
				if ( $orderby ) $this->EditBaseLink .= '&order=' . $orderby . '&ascdesc=' . $ascdesc;
				$this->EditBaseLink .= '&subsection=' . $subsection_info->Name;
				if ( $suborderby ) $this->EditBaseLink .= '&suborder=' . $suborderby . '&subascdesc=' . $subascdesc;
				$this->EditBaseLink .= '&subpk=';
	
				$this->PageLink .= '?section=' . $section_info->Name . '&pk=' . $pk;
				if ( $orderby ) $this->PageLink .= '&order=' . $orderby . '&ascdesc=' . $ascdesc;
				$this->PageLink .= '&subsection=' . $subsection_info->Name;
				if ( $suborderby ) $this->PageLink .= '&suborder=' . $suborderby . '&subascdesc=' . $subascdesc;
	
				$this->SingleEditLink .= '?section=' . $section_info->Name . '&pk=' . $pk;
				if ( $orderby ) $this->SingleEditLink .= '&order=' . $orderby . '&ascdesc=' . $ascdesc;
				$this->SingleEditLink .= '&subsection=' . $subsection_info->Name;
				if ( $suborderby ) $this->SingleEditLink .= '&suborder=' . $suborderby . '&subascdesc=' . $subascdesc;
				$this->SingleEditLink .= '&subpk=';
	
				$this->DeleteLink .= '?section=' . $section_info->Name . '&pk=' . $pk;
				if ( $orderby ) $this->DeleteLink .= '&order=' . $orderby . '&ascdesc=' . $ascdesc;
				$this->DeleteLink .= '&subsection=' . $subsection_info->Name;
				if ( $suborderby ) $this->DeleteLink .= '&suborder=' . $suborderby . '&subascdesc=' . $subascdesc;
				$this->DeleteLink .= '&subpk=';
	
				$this->AddLink .= '?section=' . $section_info->Name . '&pk=' . $pk;
				if ( $orderby ) $this->AddLink .= '&order=' . $orderby . '&ascdesc=' . $ascdesc;
				$this->AddLink .= '&subsection=' . $subsection_info->Name;
				if ( $suborderby ) $this->AddLink .= '&suborder=' . $suborderby . '&subascdesc=' . $subascdesc;
	
				if ( $subsection_info->Table->DefaultOrderByColumn() != NULL && ($suborderby != $subsection_info->Table->DefaultOrderByColumn()->SimpleName || $subascdesc != $subsection_info->Table->DefaultOrderByColumn()->DefaultAscDesc) ) {
					$this->RemoveSortLink .= 'list?section=' . $section_info->Name . '&pk=' . $pk;
					if ( $orderby ) $this->RemoveSortLink .= '&order=' . $orderby . '&ascdesc=' . $ascdesc;
					$this->RemoveSortLink .= '&subsection=' . $subsection_info->Name;
	
					$this->QueryOrderBy = $suborderby;
					$this->QueryAscDesc = $subascdesc;
				}
	
				$this->BackLink .= 'list?section=' . $section_info->Name;
				if ( $orderby ) $this->BackLink .= '&order=' . $orderby . '&ascdesc=' . $ascdesc;
	
				$this->OrderByLinkBase = 'list?section=' . $section_info->Name . '&pk=' . $pk;
				if ( $orderby ) $orderbyLinkBase .= '&order=' . $orderby . '&ascdesc=' . $ascdesc;
				$this->OrderByLinkBase .= '&subsection=' . $subsection_info->Name;
	
				foreach ( $subsection_info->Table->Columns as $column ) {
					if ( $column->Listed ) {
						$this->OrderByLinks[$column->Name] = $this->OrderByLinkBase . '&suborderby=' . $column->SimpleName . '&subascdesc=' . (($suborderby == $column->SimpleName && $subascdesc == 'asc') ? 'desc' : 'asc');
					}
				}
	
				$where = '';
				foreach ( $subsection_info->Table->Columns as $column ) {
					if ( $column->FkTable != NULL && $column->FkTable->Name == $section_info->Table->Name) $where = $column->Name . '=' . $pk;
				}

				$subsection_info->SetValues($where, $suborderby, $subascdesc, true, (($this->Page-1)*$this->ItemCount) . ',' . $this->ItemCount);
				$this->TotalCount = $subsection_info->GetTotalCount();

				foreach ( $subsection_info->Values as $value ) {
					$this->EditLinks[] = $this->EditBaseLink . $value[$subsection_info->Table->PkColumn->Name];
					$this->DeleteLinks[] = $this->DeleteLink . $value[$subsection_info->Table->PkColumn->Name];
				}
	
				$section_info->SetValue($pk, null);
	
				$this->SectionInfo = $subsection_info;
				$this->SectionInfo->ParentSection = $section_info;
			} else {
				$this->EditBaseLink .= '?section=' . $section_info->Name;
				if ( $orderby ) $this->EditBaseLink .= '&order=' . $orderby . '&ascdesc=' . $ascdesc;
				$this->EditBaseLink .= '&pk=';
	
				$this->PageLink .= '?section=' . $section_info->Name;
				if ( $orderby ) $this->PageLink .= '&order=' . $orderby . '&ascdesc=' . $ascdesc;
	
				$this->SingleEditLink .= '?section=' . $section_info->Name;
				if ( $orderby ) $this->SingleEditLink .= '&order=' . $orderby . '&ascdesc=' . $ascdesc;
				$this->SingleEditLink .= '&pk=';

				$this->DeleteLink .= '?section=' . $section_info->Name;
				if ( $orderby ) $this->DeleteLink .= '&order=' . $orderby . '&ascdesc=' . $ascdesc;
				$this->DeleteLink .= '&pk=';

				$this->AddLink .= '?section=' . $section_info->Name;
				if ( $orderby ) $this->AddLink .= '&order=' . $orderby . '&ascdesc=' . $ascdesc;

				if ( $orderby == NULL ) {
					$orderby = $section_info->Table->DefaultOrderByColumn()->SimpleName;
					$ascdesc = $section_info->Table->DefaultOrderByColumn()->DefaultAscDesc;
				}
	
				if ( $orderby != $section_info->Table->DefaultOrderByColumn()->SimpleName || ($ascdesc != $section_info->Table->DefaultOrderByColumn()->DefaultAscDesc && $ascdesc != 'asc') ) {
					$this->RemoveSortLink .= 'list?section=' . $section_info->Name;
	
					$this->QueryOrderBy = $section_info->Table->Columns->GetColumnBySimpleName($orderby)->DisplayName;
					$this->QueryAscDesc = $ascdesc;
				}
	
				$this->BackLink = '/lade';
	
				$this->SubSectionLink = 'list?section=' . $section_info->Name;
	
				$this->OrderByLinkBase = 'list?section=' . $section_info->Name;
	
				foreach ( $section_info->Table->Columns as $column ) {
					if ( $column->Listed ) {
						// TODO: change column name to name
						$this->OrderByLinks[$column->Name] = $this->OrderByLinkBase . '&orderby=' . $column->SimpleName . '&ascdesc=' . (($orderby == $column->SimpleName && $ascdesc == 'asc') ? 'desc' : 'asc');
					}
				}

				$section_info->SetValues('', $section_info->Table->GetColumnBySimpleName($orderby)->Name, $ascdesc, true, (($this->Page-1)*$this->ItemCount) . ',' . $this->ItemCount);
				$this->TotalCount = $section_info->GetTotalCount();

				foreach ( $section_info->Values as $value ) {
					//$this->EditLinks[$value] = $this->EditBaseLink . $value[$section_info->Table->PkColumn->Name];
					//$this->DeleteLinks[$value] = $this->DeleteLink . $value[$section_info->Table->PkColumn->Name];
					$this->EditLinks[] = $this->EditBaseLink . $value[$section_info->Table->PkColumn->Name];
					$this->DeleteLinks[] = $this->DeleteLink . $value[$section_info->Table->PkColumn->Name];
				}
	
				$this->SectionInfo = $section_info;
			}

			if ( $this->Page > 1 ) {
				// TODO: default item count stuff
				$this->BackLink = $this->PageLink . '&page=' . ($this->Page -1) . '&item_count=' . $this->ItemCount;
			}
		}

		$this->RenderViewContent($view);
	}
}
